/*
 * This file is part of Plop Messenger.
 *
 * Plop Messenger is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  Plop Messenger is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Plop Messenger.  If not, see <http://www.gnu.org/licenses/>.
 */
package pm;

import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Chiffreur {
	private static final String BLOWFISH = "Blowfish";

	public static byte[] decrypt(byte[] data, int off, int len, byte[] pass) throws BadPasswordException {
		try {
			data = Arrays.copyOfRange(data, off, len);
			SecretKey secretKey = new SecretKeySpec(pass, BLOWFISH);
			Cipher cipher = Cipher.getInstance(BLOWFISH);
			cipher.init(Cipher.DECRYPT_MODE, secretKey);
			return cipher.doFinal(data);
		} catch (Exception e) {
			throw new BadPasswordException();
		}
	}

	public static byte[] encrypt(byte[] data, int off, int len, byte[] pass) {
		try {
			data = Arrays.copyOfRange(data, off, len);
			SecretKey secretKey = new SecretKeySpec(pass, BLOWFISH);
			Cipher cipher = Cipher.getInstance(BLOWFISH);
			cipher.init(Cipher.ENCRYPT_MODE, secretKey);
			return cipher.doFinal(data);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
